import logging
import uuid
import random
import datetime
import time

import traceback
from flask import jsonify

from dao.db_connect import db_connect as db

internal_error = 500  # 服务器错误
success = 200  # 请求成功
data_missing = 101  # 数据库内无相关数据
identification_error = 100  # 未识别成功

code = success

# query_sql = f"select * from user where id = '3E9560B76B0849BF923CEEB368FFA7AD'"
# data = db.queryAll(query_sql)
# user_info = data[0]
# vip_expire = user_info.get('vip_expire')
# user_rank = user_info.get('rank')
# token_running_time = int(time.time()) - user_info["update_time"]
#
# print(token_running_time, user_info["update_time"])
#
# print(data)
from alipay.aop.api.AlipayClientConfig import AlipayClientConfig
from alipay.aop.api.DefaultAlipayClient import DefaultAlipayClient
from alipay.aop.api.domain.AlipayTradeQueryModel import AlipayTradeQueryModel
from alipay.aop.api.request.AlipayTradeQueryRequest import AlipayTradeQueryRequest
from alipay.aop.api.response.AlipayTradeQueryResponse import AlipayTradeQueryResponse
import controller.carbon_alipay.AlipayConfig as AlipayConfig
from controller.carbon_alipay.AlipayConfig import TradeStatus
import json

out_trade_no = '20211217110754_345345345343445345'
trade_no = None
logging.debug("working on alipay querying......")
local_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
logging.debug("current_time：" + local_time)
logging.debug("out_trade_no：" + str(out_trade_no) + ", trade_no：" + str(trade_no))

alipay_client_config = AlipayClientConfig()
alipay_client_config.format = AlipayConfig.format  # 设置请求格式
alipay_client_config.charset = AlipayConfig.charset  # 设置字符集
alipay_client_config.sign_type = AlipayConfig.signtype  # 设置请求签名类型
alipay_client_config.server_url = AlipayConfig.server_url  # 设置支付宝网关地址
alipay_client_config.app_id = AlipayConfig.appid  # 设置商户应用app_id
alipay_client_config.app_private_key = AlipayConfig.app_private_key  # 设置应用私钥
alipay_client_config.alipay_public_key = AlipayConfig.alipay_public_key  # 设置支付宝公钥

"""
得到客户端对象。
logger参数用于打印日志，不传则不打印，建议传递。
"""
logger = logging.getLogger('')
client = DefaultAlipayClient(alipay_client_config=alipay_client_config, logger=logger)

# 对照接口文档，构造请求对象
model = AlipayTradeQueryModel()
model.out_trade_no = out_trade_no
model.trade_no = trade_no

request = AlipayTradeQueryRequest(biz_model=model)
response = client.execute(request)
print(response)

# 处理响应
response = json.loads(response)
print(response)
for key in response.keys():
    print(key + ":" + response.get(key))
